public class binarytree
{
	public class Element
	{
		int num;
		Element left;
		Element right;
		public Element parent;
		
		public Element(int num, Element parent)
		{
			this.num = num;
			this.left = null;
			this.right = null;
			this.parent = parent;
		}
	}
	
	Element root;
	int lenght;
	
	public Element add(int num)
	{
		lenght++;
		if(root == null)
		{
			root = new Element(num, null);
			return root; 
		}
		
		Element tree1 = root;
		while(tree1 != null)
			if(num >= tree1.num)
				if(tree1.right == null)
				{
					tree1.right = new Element(num, tree1);
					return tree1.right;
				}
				else
					tree1 = tree1.right;
			else if(num < tree1.num)
				if(tree1.left == null)
				{
					tree1.left = new Element(num, tree1);
					return tree1.left;
				}
				else
					tree1 = tree1.left;
		
		lenght--;
		return null;
	}
	
	public void printTree(Element Elem, int level)
	{
		 if(Elem.right != null)
			 printTree(Elem.right, level+1);
		 
		 for(int k = 0; k < level; k++)
			 System.out.print("\t");
		 System.out.println(Elem.num);
		 
		 if(Elem.left != null)
			 printTree(Elem.left, level+1);
	}

	public Element max(){
		Element tree1 = root;
		while(tree1.right!=null){
			tree1 = tree1.right;
		}
		return tree1;
	}
		
	public Element min()
	{
		Element tree1 = root;
		while(tree1.left != null)
			tree1 = tree1.left;
		return tree1;
	}
	
	public static void main(String[] args)
	{
		bin tree = new bin();
		
		tree.add(7);
		tree.add(2);
		tree.add(5);
		tree.add(3);
		tree.add(9);
		tree.add(6);
		tree.add(11);
		tree.add(13);
		tree.add(1);
		
		tree.printTree(tree.root, 0);
	}
}
